<?php
namespace Home\Controller;

class ProductController extends HomeController{
    
    public function index(){
        $p=I('p',1);
        $pagesize=16;
        $p<=0 && $p=1;
        $offset=($p-1)*$pagesize;
        
        $M_producdt=M('product');
        $M_product_category=M('product_category');
        $map=array();
        $map['type'] = array('in', array('putong', 'xianzhi'));
        $map['is_show']=1;
        
        $fid=I('fid');
        $cid=I('cid');
        
        $curr_category_map=array();
        if($cid){
            $map['category_id']=$cid;
            $curr_category_map['id']=$cid;
        }else if($fid){
            $cids=$M_product_category->where(array('pid'=>$fid))->field('id')->select();
            $cids=arr2tostr($cids);
            $map['category_id']=array('in',$cids);
            
            $curr_category_map['id']=$fid;
        }else{
            $curr_category_map['id']=-1;
        }
        //当前分类
        $cur_category_list=$M_product_category->where($curr_category_map)->select();
        $this->assign('cur_category_list',$cur_category_list);
        
        $list=$M_producdt->where($map)->limit($offset,$pagesize)->field('id,title,price')->select();
        $totla=$M_producdt->where($map)->count();
        foreach($list as &$item){
            $item['image_path']=get_cover(get_table_field($item['id'],'product_id',
                'cover_id','product_image'),'path');
        }
        
        $this->assign('product_list',$list);
        $__PAGE__ = new \Think\Page($totla, $pagesize);
        $__PAGE__->lastSuffix = false;
        $__PAGE__->rollPage = 6;
        $__PAGE__->setConfig('prev', '<');
        $__PAGE__->setConfig('next', '>');
        $__PAGE__->setConfig('first', '<<');
        $__PAGE__->setConfig('last', '<span>...</span>');
        $__PAGE__->setConfig('header', '<span>共%TOTAL_PAGE%页</span>');
        $__PAGE__->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %END% %DOWN_PAGE% %HEADER%');
        
        $this->assign('_page', $__PAGE__->show());
        $this->assign('category_tree', $this->getCategoryTree());
        
        $this->display();
    }
    
    public function detail(){
        $id=I('id');
        
        $M_product=M('product');
        $M_product_image=M('product_image');
        
        //记录我足迹
        recordMybrowse($id);
        $info=$M_product->find($id);
        $M_product->where(array('id'=>$id))->setInc('view',1);  
        
        if($info['type']=='paipin'){            
           $this->redirect('Pzb/auctionDetail',array('id'=>$id));
        }
        
        empty($info) && $this->error('商品不存在',U('Index'));
        
        $images =$M_product_image ->where(array('product_id'=>$info['id']))
        ->order('id ASC')->select();
        
        $latpath=array();
        foreach ($images as &$item){
           $item['path']= get_cover($item['cover_id'],'path');
        }
       
        $info['images']=$images;        
        $info['category_title']=get_table_field($info['category_id'],'id','title','product_category');
        //热销
        $saletopmap=array();
        $saletopmap['type']='putong';
        $saletopmap['in_special_panic']=1;
        $saletopmap_list=$M_product->where($saletopmap)->limit(0,6)->order('sales DESC')->select();
        
        foreach ($saletopmap_list as &$item) {
            $item['price']=(int)$item['price'];
            $image_info = $M_product_image->where(array('product_id'=>$item['id']))
            ->order('id ASC')->find();
            $item['image'] = get_cover($image_info['cover_id'],'path');
        }
        $M_product_comment=M('product_comment');
        $info['comment_total']=$M_product_comment->where(array('product_id'=>$id))->count()|0;
        $info['comment_rate1']=$M_product_comment->where(array('product_id'=>$id,'rate'=>1))->count()|0;
        $info['comment_rate2']=$M_product_comment->where(array('product_id'=>$id,'rate'=>2))->count()|0;
        $info['comment_rate3']=$M_product_comment->where(array('product_id'=>$id,'rate'=>3))->count()|0;
        
        
        $this->assign('salestop',$saletopmap_list);        
        
        $this->assign('info',$info);
        $this->display();
        
    }
    
    
    /**
     * 获取商品评论
     * 
     * @param integer page 页码
     * @return Ambigous <multitype:, mixed>
     */
    public function getComments(){
        
        $product_id = I('request.product_id', 0);
        $level = I('request.level', 1);
        $p = I('request.p', 1);
        $p<=0 && $p=1;
        $pagesize =8;
        $offset = ($p - 1) * $pagesize;
     
        $comments = array();
         
        $M_product_comment=M('product_comment');
        $M_product_comment_message=M('product_comment_message');
        $map=array();
        $map['product_id']=$product_id;
        if($level){
            $map['rate']=$level;
        }
        
        $comments=$M_product_comment->limit($offset,$pagesize)->where($map)
        ->select();
        $total=$M_product_comment->where($map)->count();
        
       
        foreach($comments as &$item){
          
            //$item['avatar']=get_avatar($item['uid']);
            $nickname=get_nickname($item['uid']);   
            
            $item['nickname']=substr_cut($nickname);
           
            $item['format_time']=time_format($item['create_time']);
            $images=array();
            $images_id=explode(',', $item['cover_ids']);
          
            $re_msg=$M_product_comment_message->where(array('comment_id'=>$item['id'],'is_saler'=>1))->select();
            
            foreach($re_msg as $rec_item){
                $item['recomment'][]=$rec_item['content'];
            }
            
            foreach($images_id as $cover_id){
                if(get_cover($cover_id,'path')){
                $item['images'][]=get_cover($cover_id,'path');
                }
            }
            
        }
       
        $result=array();
        $result['total']=$total;
       
        $__PAGE__ = new \Think\Page($total, $pagesize);
        $__PAGE__->lastSuffix = false;
        $__PAGE__->rollPage = 6;
        $__PAGE__->setConfig('prev', '<');
        $__PAGE__->setConfig('next', '>');
        $__PAGE__->setConfig('first', '<<');
        $__PAGE__->setConfig('last', '<span>...</span>');
        $__PAGE__->setConfig('header', '<span>共%TOTAL_PAGE%页</span>');
        $__PAGE__->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %END% %DOWN_PAGE% %HEADER%');
        $__PAGE__->setConfig('ajax-url', true);
        
        $this->ajaxReturn(array('results'=>$comments, 'pages'=>$__PAGE__->show()));
    }
    
    /**
     * 获取商品分类树
     *
     * @return Ambigous <multitype:, mixed>
     */
    private function getCategoryTree(){
        static $list = array();
    
        if (empty($list)) {
            $list = S('CACHE_ADMIN_PRODUCT_CATEGORY');
        }
    
        if (empty($list)) {
            $M_product_category = M('product_category');
            $list = $M_product_category->order('sort ASC')->select();
            $list = list_to_tree($list);
    
            S('CACHE_ADMIN_PRODUCT_CATEGORY', $list);
        }
    
        return $list;
    }
}
?>